package org.jxy.study.rpc.netty.hander;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import org.jxy.study.rpc.message.DataMsg;

import java.util.List;

/**
 * @author jxy
 * @className ByteMsgToDataMsgDecoder
 * @description 使用ReplayingDecoder帮助处理tcp粘包、拆包问题
 * @date 2021/3/10 21:53
 */
public class ByteMsgToDataMsgDecoder extends ReplayingDecoder<DataMsg> {
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
        int length = in.readInt();
        byte[] content = new byte[length];
        in.readBytes(content);
        DataMsg msg = new DataMsg(length, content);
        out.add(msg);
    }
}
